package com.henu.xyh;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RadioButton;

import com.henu.adapter.Listexpresswadapter;
import com.henu.R;

import com.henu.xyh.entity.Express;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class Confession_WallActivity extends AppCompatActivity {


    public ListView listview;
    public ArrayList<Express> expresseslist=new ArrayList<>();
    Listexpresswadapter adapter;
    private RadioButton radiobtn;
    private ImageButton btn_wall_return;
    @Override
    //河大论坛
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_confession__wall);

        btn_wall_return= (ImageButton) findViewById(R.id.expresswall_return);
        btn_wall_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        listview= (ListView) findViewById(R.id.listview_express);
        adapter=new Listexpresswadapter(this);
        listview.setAdapter(adapter);
        Bmob.initialize(this, "a24db0fac16b375a97072b85080ea20f");
        // 写入类别数据

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent=new Intent(Confession_WallActivity.this,Express_ReplyActivity.class);
                Express express=expresseslist.get(i);
                intent.putExtra("listview",express);
                startActivity(intent);
            }
        });
        radiobtn= (RadioButton) findViewById(R.id.edit_content);
        radiobtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(Confession_WallActivity.this,PublishActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        BmobQuery<Express> query = new BmobQuery<Express>();
        //返回50条数据，如果不加上这条语句，默认返回10条数据
        query.setLimit(50);
        query.include("User_Id");
        query.order("-createdAt");
        query.findObjects(new FindListener<Express>() {
            @Override
            public void done(List<Express> object, BmobException e) {
                if(e==null){
                    expresseslist=(ArrayList)object;
                    adapter.addData(expresseslist); // 绑定数据
                    adapter.notifyDataSetChanged();// 显示
                }else{
                    System.out.println("bmob失败："+e.getMessage()+","+e.getErrorCode());
                }
            }
        });
    }
}
